<?php

namespace app\models;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveQuery;

/**
 * 菜单模型
 * @property int $id 主键
 * @property int $parent_id 上级
 * @property int $item_id 权限ID
 * @property string $name 名称
 * @property string $icon 图标
 * @property string $route 路由
 * @property int $type 类型
 * @property int $target 打开方式
 * @property int $status 状态
 * @property int $level 层级
 * @property string $remark 备注
 * @property int $sort 排序
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 *
 * @property $parent
 */
class Menu extends ActiveRecord
{
    const TYPE_DIR = 1;         //  目录
    const TYPE_MENU = 2;        //  菜单
    const TYPE_BUTTON = 3;      //  按钮

    const TARGET_NEW = 1;       //  新窗口
    const TARGET_SELF = 2;      //  当前窗口
    const TARGET_MODAL = 3;     //  弹窗

    public static function tableName()
    {
        return '{{%menu}}';
    }

    public static function find()
    {
        return new ActiveQuery(get_called_class());
    }

    public function behaviors()
    {
        return [['class' => TimestampBehavior::class]];
    }

    /**
     * 查询上级权限项
     * @return array|\yii\db\ActiveRecord|null
     */
    public function getParent()
    {
        return self::find()->where(['id' => $this->parent_id])->one();
    }

    public function getChildren()
    {
        return $this->hasMany(Menu::class, ['parent_id' => 'id']);
    }

    public function getChildrenCount()
    {
        return $this->hasMany(Menu::class, ['parent_id' => 'id'])
            ->count('id');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                ['parent_id', 'item_id', 'type', 'target', 'level', 'status', 'sort', 'created_at', 'updated_at'],
                'integer'
            ],
            [['name', 'route', 'type', 'target', 'level', 'remark', 'created_at', 'updated_at'], 'required'],
            [['name', 'icon', 'route'], 'string', 'max' => 32],
            [['remark'], 'string', 'max' => 64],
            [['path'], 'string', 'max' => 16],
        ];
    }


    /**
     * 新增时，设置 path 值
     * @param bool $insert
     * @param array $changedAttributes
     */
    public function afterSave($insert, $changedAttributes)
    {
        if ($insert) {
            $parent = $this->getParent();
            $this->path = $this->parent_id ? $parent->path . '_' . $this->id : $this->id;
            $this->level = $this->parent_id ? $parent->level + 1 : 1;
            $this->save(false);
        }
    }


    /**
     * 打开方式列表
     * @return string[]
     */
    public static function getTargetMapping()
    {
        return [
            ['id' => self::TARGET_NEW, 'name' => '新窗口'],
            ['id' => self::TARGET_SELF, 'name' => '当前窗口'],
            ['id' => self::TARGET_MODAL, 'name' => '弹窗'],
        ];
    }

    /**
     * 类型列表
     * @return string[]
     */
    public static function getTypeMapping()
    {
        return [
            ['id' => self::TYPE_DIR, 'name' => '目录'],
            ['id' => self::TYPE_MENU, 'name' => '菜单'],
            ['id' => self::TYPE_BUTTON, 'name' => '按钮'],
        ];
    }

    /**
     * 显示类型
     * @param $type
     * @return string
     */
    public static function showTypeText($type)
    {
        $typeArr = self::getTypeMapping();
        $typeArr = array_column(array_column($typeArr, null, 'id'), 'name');
        return isset($typeArr[$type]) ? $typeArr[$type] : '-';
    }

    /**
     * 显示打开方式
     * @param $target
     * @return string
     */
    public static function showTargetText($target)
    {
        $targetArr = self::getTargetMapping();
        $targetArr = array_column(array_column($targetArr, null, 'id'), 'name');
        return isset($targetArr[$target]) ? $targetArr[$target] : '-';
    }
}